/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.entities;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


@Entity
@Table(name = "event")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Event.findAll", query = "SELECT e FROM Event e"),
    @NamedQuery(name = "Event.findByIde", query = "SELECT e FROM Event e WHERE e.ide = :ide"),
    @NamedQuery(name = "Event.findByDescription", query = "SELECT e FROM Event e WHERE e.description = :description"),
    @NamedQuery(name = "Event.findByVisibility", query = "SELECT e FROM Event e WHERE e.visibility = :visibility"),
    @NamedQuery(name = "Event.findByWeather", query = "SELECT e FROM Event e WHERE e.weather = :weather"),
    @NamedQuery(name = "Event.findByTemperature", query = "SELECT e FROM Event e WHERE e.temperature = :temperature"),
    @NamedQuery(name = "Event.findByType", query = "SELECT e FROM Event e WHERE e.type = :type"),
    @NamedQuery(name = "Event.findByName", query = "SELECT e FROM Event e WHERE e.name = :name"),
    @NamedQuery(name = "Event.findByStartHour", query = "SELECT e FROM Event e WHERE e.startHour = :startHour"),
    @NamedQuery(name = "Event.findByEndHour", query = "SELECT e FROM Event e WHERE e.endHour = :endHour"),
    @NamedQuery(name = "Event.findByCity", query = "SELECT e FROM Event e WHERE e.city = :city"),
    @NamedQuery(name = "Event.findByAddress", query = "SELECT e FROM Event e WHERE e.address = :address"),
    @NamedQuery(name = "Event.findValidOutdoor", query = "SELECT e FROM Event e WHERE e.valid='true' AND e.type='outdoor'")})
public class Event implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "IDE")
    private Integer ide;
    @Size(max = 255)
    @Column(name = "Description")
    private String description;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 7)
    @Column(name = "Visibility")
    private String visibility;
    @Size(max = 10)
    @Column(name = "Weather")
    private String weather;
    @Size(max = 3)
    @Column(name = "Temperature")
    private String temperature;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 8)
    @Column(name = "Type")
    private String type;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "Name")
    private String name;
    @Basic(optional = false)
    @NotNull
    @Column(name = "StartHour")
    @Temporal(TemporalType.TIMESTAMP)
    private Date startHour;
    @Basic(optional = false)
    @NotNull 
    @Column(name = "EndHour")
    @Temporal(TemporalType.TIMESTAMP)
    private Date endHour;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "City")
    private String city;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "Address")
    private String address;
    @Basic(optional = false)
    @NotNull
    @Column(name = "Valid")
    private boolean valid;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "eventID")
    private List<Notification> notificationList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "event")
    private List<Invitation> invitationList;
    @JoinColumn(name = "OrganizerID", referencedColumnName = "IDU")
    @ManyToOne(optional = false)
    private User organizerID;

    public Event() {

    }

    public Event(Integer ide) {
        this.ide = ide;
    }

    public Event(Integer ide, String visibility, String type, String name, 
            Date startHour, Date endHour, String city, String address, String description, boolean v) {
        this.ide = ide;
        this.visibility = visibility;
        this.type = type;
        this.name = name;
        this.startHour = startHour;
        this.endHour = endHour;
        this.city = city;
        this.address = address;
        this.description = description;
        this.valid = v;
    }

    public Integer getIde() {
        return ide;
    }

    public void setIde(Integer ide) {
        this.ide = ide;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getVisibility() {
        return visibility;
    }

    public void setVisibility(String visibility) {
        this.visibility = visibility;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public String getTemperature() {
        return temperature;
    }

    public void setTemperature(String temperature) {
        this.temperature = temperature;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getStartHour() {
        return startHour;
    }
    
    public String getFormatStartDate(){
        Locale.setDefault(Locale.ENGLISH);
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
        String dataString = sdf.format(this.startHour);
        return dataString;
    }
    
    public String getFormatStartHour(){
        SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
        String hourString = sdf.format(this.startHour);
        return hourString;
    }    
    
    public String getFormatEndDate(){
        Locale.setDefault(Locale.ENGLISH);
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
        String dataString = sdf.format(this.endHour);
        return dataString;
    }
    
    public String getFormatEndHour(){
        SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
        String hourString = sdf.format(this.endHour);
        return hourString;
    }

    public void setStartHour(Date startHour) {
        this.startHour = startHour;
    }

    public Date getEndHour() { 
        return endHour;
    }

    public void setEndHour(Date endHour) {
        this.endHour = endHour;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
   
    public boolean getValid() {
        return valid;
    }

    public void setValid(boolean v) {
        this.valid = v;
    }    

    @XmlTransient
    public List<Notification> getNotificationList() {
        return notificationList;
    }

    public void setNotificationList(List<Notification> notificationList) {
        this.notificationList = notificationList;
    }

    @XmlTransient
    public List<Invitation> getInvitationList() {
        return invitationList;
    }

    public void setInvitationList(List<Invitation> invitationList) {
        this.invitationList = invitationList;
    }

    public User getOrganizerID() {
        return organizerID;
    }

    public void setOrganizerID(User organizerID) {
        this.organizerID = organizerID;
    }

    public List<User> getInvitedUsers(){
        List<User> result = new ArrayList<>();
        for(Invitation i : this.invitationList){
            result.add(i.getUser());
        }
        System.out.println("RESULT "+result);
        return result;
    }
    
    public List<User> getParticipantUsers(){
        List<User> result = new ArrayList<>();
        for(Invitation i : this.invitationList){
            if(i.getParticipant()){
                result.add(i.getUser());
            }
        }
        return result;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (ide != null ? ide.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Event)) {
            return false;
        }
        Event other = (Event) object;
        if ((this.ide == null && other.ide != null) || (this.ide != null && !this.ide.equals(other.ide))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "it.polimi.meteocal.entities.Event[ ide=" + ide + " ]";
    }
    
}
